package com.ereport.dao;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.ereport.entity.NewsUpdate;
import com.ereport.entity.PublishNews;
import com.ereport.util.ApplicationContext;

public class NewsUpdateDao {
static Transaction tx;
	
	private  void endOperations(Session session) {
		
		session.close();
		session=null;
	}

	private  Session startOperations()throws Exception {
		Session session = ApplicationContext.getHiberSession().openSession();
		tx=session.beginTransaction();
		return session;
		
	}

	public int testHeadline(String headLine) throws Exception {
		Session session=startOperations();
		SQLQuery qry=session.createSQLQuery("select count(*) as count from News_Update where head_lines=:headLine");
		qry.setString("headLine", headLine);
		qry.addScalar("count", Hibernate.INTEGER);
		int c=(Integer)qry.uniqueResult();
		endOperations(session);
		return c;
	}

	public int insert(NewsUpdate nu) throws Exception {
		Session session = startOperations();
		session.saveOrUpdate(nu);
		session.getTransaction().commit();
		endOperations(session);
		return 0;
		
	}

	public NewsUpdate getMaxId() throws Exception {
		Session session=startOperations();
		Query qry=session.createQuery("from NewsUpdate where id=(select max(id) from NewsUpdate)");
		NewsUpdate c=(NewsUpdate)qry.uniqueResult();
		endOperations(session);
		return c;
	}

	public int insertPublish(List<PublishNews> pnlist) throws Exception {
		Session session = startOperations();
		for (PublishNews pr: pnlist) {
	           session.saveOrUpdate(pr);
	           session.getTransaction().commit();
	        }
		endOperations(session);
		return 0;
		
		
	}

	public List<NewsUpdate> getAllHeadlines() throws Exception {
		Session session=startOperations();
		Query qry=session.createQuery("from NewsUpdate where is_delete=false");
		List<NewsUpdate> nulist=(List<NewsUpdate>)qry.list();
		endOperations(session);
		return nulist;
	}

	public List<PublishNews> getPublishNewsByNewsId(int newid) throws Exception {
		Session session=startOperations();
		Query qry=session.createQuery("from PublishNews where news_id=:id ");
		qry.setInteger("id", newid);
		List<PublishNews> nulist=(List<PublishNews>)qry.list();
		endOperations(session);
		return nulist;
	}

	public NewsUpdate findById(int newid) throws Exception {
		Session session=startOperations();
		Query qry=session.createQuery("from NewsUpdate where id=:id");
		qry.setInteger("id", newid);
		NewsUpdate c=(NewsUpdate)qry.uniqueResult();
		endOperations(session);
		return c;
	}

	public void deleteNews(List<Integer> nulist) throws Exception {
		Session session=startOperations();
		for(Integer i:nulist){
		Query qry=session.createQuery("Update NewsUpdate set is_delete=true where id=:id");
		qry.setInteger("id", i);
		qry.executeUpdate();
		}
		endOperations(session);		
	}

	public List<NewsUpdate> getAllHeadlinesOfExecutive(int id) throws Exception {
		Session session=startOperations();
		Query qry=session.createQuery("select distinct n from NewsUpdate n, PublishNews p where n.id=p.newsId and p.publishedTo=:id and n.isDelete=false");
		qry.setInteger("id", id);
		List<NewsUpdate> nulist=(List<NewsUpdate>)qry.list();
		endOperations(session);
		return nulist;
	}
	
}	